//
//  ActivityListViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivityListViewController.h"
#import "AppTools.h"
#import "Activity.h"
#import "ActivityListTableViewCell.h"
#import "ActivityDetailViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "AFNetworking.h"
#import "DataBaseSingleton.h"
@interface ActivityListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *activityTableView;
@property (nonatomic, retain)NSMutableArray *ActivityArray;
@property (nonatomic, retain) MBProgressHUD *hub;

@end

@implementation ActivityListViewController

- (void)dealloc
{
    [_ActivityArray release];
    [_activityTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.activityTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    
    self.activityTableView.delegate = self;
    
    self.activityTableView.dataSource = self;
    
    [self.view addSubview:self.activityTableView];
    
    [self.activityTableView release];
    
    [self.activityTableView registerClass:[ActivityListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.title = @"活动";
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
    
    [self addHeaderRefresh];
    [self addFooterRefresh];
    [self getAFNetWorkingData];
}

-(void)getAFNetWorkingData
{
    self.ActivityArray = [NSMutableArray array];
    
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
    [[DataBaseSingleton shareDataBase] openDB];
    [[DataBaseSingleton shareDataBase] dropTable];
    [[DataBaseSingleton shareDataBase] createActivityListTable];
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSMutableArray *array = [dic objectForKey:@"events"];
        
        for (NSDictionary *dic in array) {
            
            Activity *activity = [[Activity alloc] init];
            [activity setValuesForKeysWithDictionary:dic];

            [[DataBaseSingleton shareDataBase] insertActivityListSQL:activity];

        }
        
        self.ActivityArray = [NSMutableArray arrayWithArray:[[DataBaseSingleton shareDataBase] selectActivityList]];
        
        [self.activityTableView headerEndRefreshing];
        [self.activityTableView footerEndRefreshing];
        [self.activityTableView reloadData];
        [self.hub hide:YES];
        
    }];

}

-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    [self.activityTableView addHeaderWithCallback:^{
        
        [self getAFNetWorkingData];
        
    }];
//    [self.activityTableView headerBeginRefreshing];
}

-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.activityTableView addFooterWithCallback:^{
        
        
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return self.ActivityArray.count;
}

// 设置cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    ActivityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    Activity *activity = [self.ActivityArray objectAtIndex:indexPath.row];
    
    cell.aActivity = activity;

    return cell;
    
}



// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ActivityDetailViewController *activityVC = [[ActivityDetailViewController alloc] init];
    
    Activity *activi = [self.ActivityArray objectAtIndex:indexPath.row];
    
    activityVC.act = activi;
    
    [self.navigationController pushViewController:activityVC animated:YES];
    
    [activityVC release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
